package com.cloud.bc.service.impl;

import com.cloud.bc.model.entity.BcFriendApplys;
import com.cloud.bc.mapper.BcFriendApplysMapper;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.service.BcFriendApplysService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.bc.service.BcSinglesService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 朋友申请
 *
 * @author douhaichao code generator
 * @date 2021-01-12 10:13:01
 */
@Service
@AllArgsConstructor
public class BcFriendApplysServiceImpl extends ServiceImpl<BcFriendApplysMapper, BcFriendApplys> implements BcFriendApplysService {

    private final BcSinglesService bcSinglesService;

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public BcFriendApplys selectOne(BcFriendApplys req) {
        QueryWrapper queryWrapper = new QueryWrapper<BcFriendApplys>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<BcFriendApplys> selectAll(BcFriendApplys req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<BcFriendApplys> selectPage(Page page, BcFriendApplys req) {
        IPage<BcFriendApplys> list = baseMapper.selectPage(
                page,
                new QueryWrapper<BcFriendApplys>().setEntity(req));
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<BcFriendApplys> selectPageByLike(Page page, BcFriendApplys req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getId()) {
            queryWrapper.eq("id", req.getId());
        }

        if (null != req.getUserId()) {
            queryWrapper.eq("user_id", req.getUserId());
        }

        if (null != req.getTargetId()) {
            queryWrapper.eq("target_id", req.getTargetId());
        }

        if (!StrUtil.isEmpty(req.getPaytype())) {
            queryWrapper.like("paytype", req.getPaytype());
        }

        if (null != req.getCash()) {
            queryWrapper.eq("cash", req.getCash());
        }

        if (null != req.getAgree()) {
            queryWrapper.eq("agree", req.getAgree());
        }

        if (null != req.getDisposeTime()) {
            queryWrapper.eq("dispose_time", req.getDisposeTime());
        }

        if (null != req.getCreateTime()) {
            queryWrapper.eq("create_time", req.getCreateTime());
        }

        if (null != req.getMotifyTime()) {
            queryWrapper.eq("motify_time", req.getMotifyTime());
        }
        IPage<BcFriendApplys> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(BcFriendApplys req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public BcFriendApplys selectFriendStatus(BcFriendApplys bcFriendApplys) {
        return baseMapper.selectFriendStatus(bcFriendApplys);
    }

    @Override
    public List<BcSingles> selectNewFriendList(Page page, Integer userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("target_id", userId);
        IPage iPage = baseMapper.selectPage(page, queryWrapper);
        List<BcFriendApplys> records = iPage.getRecords();
        if (null == records || records.size() <= 0) {
            return null;
        }
        Set<Integer> userIds = records.stream().map(t -> t.getUserId()).collect(Collectors.toSet());
        QueryWrapper queryWrapperSingle = new QueryWrapper();
        queryWrapperSingle.in("user_id", userIds);
        List<BcSingles> list = bcSinglesService.list(queryWrapperSingle);
        //回写审核状态
        for (BcSingles bcSingles : list) {
            BcFriendApplys bcFriendApplys =
                    records.stream().filter(t -> t.getUserId().equals(bcSingles.getUserId())).
                            findFirst().orElse(new BcFriendApplys());
            bcSingles.setIsAuth(bcFriendApplys.getAgree());
        }
        //回写审核状态
        return list;
    }
}
